package com.amc.controllers;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.amc.dto.FoodView;
import com.amc.entity.FoodType;
import com.amc.entity.Merchant;
import com.amc.service.IMerchantService;

@Controller
@RequestMapping("/merchant")
public class MerchantListController {

	private IMerchantService service;

	@Resource(name = "merchantService")
	public void setService(IMerchantService service) {
		this.service = service;
	}

	// 菜品下拉框初始化
	@RequestMapping("/foodtypelist")
	@ResponseBody
	public List<FoodType> initFoodTypeList() {
		List<String> merchantsId = service.requestMerchantsId();
		// List<String> merchantsId = new ArrayList<>();
		// merchantsId.add("8a5e9d115de0dae9015de0daeb9e0000");
		// merchantsId.add("8a5e9d115de0dae9015de0daeb9e0001");
		// merchantsId.add("8a5e9d445de0df20015de0df24290000");
		List<Merchant> merchants = service.queryAllMerchants(merchantsId);
		return service.queryFoodTypeByMerchantList(merchants);
	}

	// 所有商家初始化
	@RequestMapping("/merchantlist")
	@ResponseBody
	public List<Merchant> initMerchantList() {
		// List<String> merchantsId = new ArrayList<>();
		// merchantsId.add("8a5e9d115de0dae9015de0daeb9e0000");
		// merchantsId.add("8a5e9d115de0dae9015de0daeb9e0001");
		// merchantsId.add("8a5e9d445de0df20015de0df24290000");
		List<String> merchantsId = service.requestMerchantsId();
		return service.queryAllMerchants(merchantsId);
	}

	// 广告菜列表初始化
	@RequestMapping("/advertisement")
	@ResponseBody
	public List<FoodView> initAdvertisement() {
		List<FoodView> foodsId = service.requestAdv();
		return foodsId;
	}

	// 点击菜品类型更新商家列表
	@RequestMapping("/update")
	@ResponseBody
	public List<Merchant> updateMerchantList(String foodTypeId) {
		List<String> merchantsId = service.requestMerchantsId();
		List<Merchant> merchants = service.queryAllMerchants(merchantsId);
		List<Merchant> list = service.queryMerchantByFoodType(foodTypeId,
				merchants);
		return list;
	}

	// 商家搜索
	@RequestMapping("/search")
	@ResponseBody
	public List<Merchant> searchMerchants(String keyword)
			throws UnsupportedEncodingException {
		keyword = new String(keyword.getBytes("ISO-8859-1"), "UTF-8");
		List<Merchant> list = new ArrayList<>();
		list = service.queryMerchants(keyword);
		return list;
	}

	// 商家搜索
	@RequestMapping("/scope")
	@ResponseBody
	public double merchantScope(String merchantId) {
		return service.calculateSore(merchantId);
	}
}
